package com.heima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.domain.HhPatient;
import com.heima.domain.HhUsers;
import com.heima.dto.SaveDto;
import com.heima.mapper.HhPatientMapper;
import com.heima.service.IHhPatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.thread.UserThreadLocal;
import com.heima.vo.PatientVo;
import com.heima.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 就诊人 服务实现类
 * </p>
 *
 * @author 黑马程序员
 * @since 2023-01-29
 */
@Service
public class HhPatientServiceImpl extends ServiceImpl<HhPatientMapper, HhPatient> implements IHhPatientService {

    @Autowired
    private HhPatientMapper hhPatientMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;



    /**
     * 就诊人列表
     *
     * @return 就诊人列表
     */
    @Override
    public List<PatientVo> getList() {

        // 查询当前登陆用户
        Integer userId = UserThreadLocal.getUserLocal();

        // 根据当前登录用户，查询对应的就诊人信息

        List<PatientVo> voList = new ArrayList<>();

        LambdaQueryWrapper<HhPatient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HhPatient::getUserId, userId);
        wrapper.orderByDesc(HhPatient::getIsDefault);
        List<HhPatient> list = hhPatientMapper.selectList(wrapper);

        // 根据前端的要求，组装返回值结构
        if (!CollectionUtil.isEmpty(list())) {
            for (HhPatient hhPatient : list) {
                if (hhPatient == null) {
                    continue;
                }
                PatientVo patientVo = new PatientVo();
                BeanUtils.copyProperties(hhPatient, patientVo);
                String cardNumber = hhPatient.getCardNumber();
                Integer cardType = hhPatient.getCardType();
                if (cardType == 0) {
                    patientVo.setCardNumber(DesensitizedUtil.idCardNum(cardNumber, 4, 4));
                }
                voList.add(patientVo);
            }
        }


        // 把mysql查出来的数据，写到Redis
        redisTemplate.opsForValue().set("patientList:user_" + userId, JSON.toJSONString(voList));

        return voList;
    }

    /**
     * 获取就诊人信息
     *
     * @param patientId 就诊人id
     * @return    HhPatient 就诊人信息
     */
    @Override
    public HhPatient getPatientById(Integer patientId) {
        return hhPatientMapper.selectById(patientId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void delPatientById(Integer patientId) {
        // 查询当前登陆用户
        Integer userId = UserThreadLocal.getUserLocal();

        int res = hhPatientMapper.deleteById(patientId);

        if (res != 1) {
            throw new RuntimeException("删除失败");
        }
        // 移除Redis数据
        redisTemplate.delete("patientList:user_" + userId);
    }

    @Override
    public void addPatient(SaveDto saveDto) {
        HhPatient hhUsers = new HhPatient();
        BeanUtils.copyProperties(saveDto, hhUsers);
        hhUsers.setUserId(UserThreadLocal.getUserLocal());
        int res = hhPatientMapper.insert(hhUsers);
        if (res != 1){
            throw new RuntimeException("添加就诊人失败");
        }
    }

    @Override
    public void updatePatientById(Integer id,SaveDto saveDto) {
        HhPatient hhUsers = new HhPatient();
        BeanUtils.copyProperties(saveDto, hhUsers);
        LambdaQueryWrapper<HhPatient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HhPatient::getId, id);
        int res = hhPatientMapper.update(hhUsers,wrapper);
        if (res != 1){
            throw new RuntimeException("修改就诊人失败");
        }

    }


}
